<?php

namespace App\Console\Commands\Check;

use App\Library\Common\DateTool;
use App\Models\Check\CheckCmbBankFlow;
use App\Service\Check\CmbBank\CloudDirectConnectClient;
use App\Service\Check\CmbBank\CmbAccountService;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class SyncCmbBankFlow extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'check:sync_cmb_bank_flow {fromDate?} {toDate?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '同步招商银行流水';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $fromDate = $this->argument('fromDate');
        $toDate   = $this->argument('toDate');
        if (!DateTool::isDate($fromDate)) {
            $fromDate = Carbon::yesterday()->subDays(7)->toDateString();
        }
        if (!DateTool::isDate($toDate)) {
            $toDate = Carbon::yesterday()->toDateString();
        }
        while (strtotime($fromDate) <= strtotime($toDate)) {
            $count = CmbAccountService::syncIncomeFlows($fromDate);
            echo "----  $fromDate  ----  {$count}个  \n";
            $fromDate = Carbon::parse($fromDate . ' 00:00:00')->addDay()->toDateString();
        }
        return 0;
    }

}
